self.onmessage = function(e) {
    const { method, url, body } = e.data;

    let fetchOptions = {
        method: method,
        mode: "cors" // 确保跨域请求
    };

    if (method !== "GET") {
        fetchOptions.headers = {
            "Content-Type": "application/json"
        };
        fetchOptions.body = JSON.stringify(body);
    }

    fetch(url, fetchOptions)
        .then(response => {
            if (!response.ok) {
                throw new Error("Network response was not ok " + response.statusText);
            }
            return response.json();
        })
        .then(data => self.postMessage(data))
        .catch(error => self.postMessage({ error: error.message }));
};